package practice.unionfind;

/**
 * @Description:
 * @Author Ammar
 * @Create 2023/8/26 19:32
 */
public class QuickUnion extends AbstractUnionFind {
    public QuickUnion(int capacity) {
        super(capacity);
    }

    @Override
    public int find(int v) {
        rangeCheck(v);
        while (parent[v] != v) {
            v = parent[v];
        }
        return v;
    }

    @Override
    public void union(int v1, int v2) {
        int p1 = find(v1);
        int p2 = find(v2);
        if (p1 == p2) return;
        parent[p2] = p1;
    }
}
